package com.cy.pj.common.aspect;

import com.cy.pj.common.annotation.ClearCache;
import com.cy.pj.common.annotation.RequiredCache;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
@Aspect
public class SysModuleCacheAspect {//Cache

    /**
     * 这里希望每个业务模块都有自己对应的cache
     * 1.外层map的key为具体模块的cache
     * 2.内层模块key为存储数据时使用的key*/
    private Map<String,Map<String,Object>> cacheMap=new ConcurrentHashMap<>();
    //@annotation
    @Pointcut("@annotation(com.cy.pj.common.annotation.RequiredCache)")//细粒度切入点表达式
    public void doCache(){}

    @Pointcut("@annotation(com.cy.pj.common.annotation.ClearCache)")
    public void doClearCache(){}
    @AfterReturning("doClearCache()")
    public void doAfterReturning(JoinPoint joinPoint) throws Exception {
        String cacheName=getCacheName(ClearCache.class,joinPoint);
        Map<String,Object> cache=cacheMap.get(cacheName);
        cache.clear();
    }

    private Method getTargetMethod(Class<? extends Annotation> annoClass, JoinPoint joinPoint)throws Exception{
        Class<?> targetClass=joinPoint.getTarget().getClass();
        MethodSignature ms=(MethodSignature) joinPoint.getSignature();
        Method targetMethod= targetClass.getMethod(ms.getName(), ms.getParameterTypes());
        return targetMethod;
    }

    private String getCacheName(Class<? extends Annotation> annoClass, JoinPoint joinPoint) throws Exception {
       // Class<?> tagetClass=joinPoint.getTarget().getClass();
        //MethodSignature ms=(MethodSignature)joinPoint.getSignature();
       // Method targetMethod=tagetClass.getMethod(ms.getName(),ms.getParameterTypes());
        Method targetMethod=getTargetMethod(annoClass, joinPoint);
        Annotation annotation=targetMethod.getAnnotation(annoClass);
        String cacheName=null;
        if (annotation instanceof RequiredCache){
            RequiredCache requiredCache=(RequiredCache) targetMethod.getAnnotation(annoClass);
            cacheName=requiredCache.name();
        }else if (annotation instanceof ClearCache){
            ClearCache clearCache=(ClearCache)targetMethod.getAnnotation(annoClass);
            cacheName=clearCache.name();
        }
        return cacheName;
    }

    private String getCacheKey(Class<? extends Annotation> annoClass, JoinPoint joinPoint) throws Exception {
       // Class<?> tagetClass=joinPoint.getTarget().getClass();
       // MethodSignature ms=(MethodSignature)joinPoint.getSignature();
       // Method targetMethod=tagetClass.getMethod(ms.getName(),ms.getParameterTypes());
        Method targetMethod=getTargetMethod(annoClass, joinPoint);
        Annotation annotation=targetMethod.getAnnotation(annoClass);
        String key=null;
        if (annotation instanceof RequiredCache){
            RequiredCache requiredCache=(RequiredCache) targetMethod.getAnnotation(annoClass);
            key=requiredCache.key();
        }
        return key;
    }


    @Around("doCache()")//@Around中方法的参数连接点的类型只能写ProceedingJoinPoint
    public Object doCacheAround(ProceedingJoinPoint joinPoint)throws Throwable{
        System.out.println("Get Data From Cache");
        String cacheName=getCacheName(RequiredCache.class,joinPoint);
        Map<String,Object> cache=cacheMap.get(cacheName);
        if (cache==null){
            cache=new ConcurrentHashMap<>();
            cacheMap.put(cacheName, cache);
        }
        String key=getCacheKey(RequiredCache.class, joinPoint);
        Object result=cache.get(key);
        if (result!=null)return result;
        result=joinPoint.proceed();
        System.out.println("Put data to cache");
        cache.put(key, result);
        return result;
    }

}
